﻿using System;

namespace Data.Common
{
    public static class Converts
    {
        /// <summary>
        ///     转换数字
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static decimal ConvertToDecimal(string num)
        {
            decimal result;
            try
            {
                result = Convert.ToDecimal(num);
            }
            catch (Exception)
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        ///     转换成数字
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int ConvertToInt(string num)
        {
            int result;
            try
            {
                result = Convert.ToInt32(num);
            }
            catch (Exception)
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 转换为长整形
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static long ConvertToLong(string num)
        {
            long result = 0;
            try
            {
                result = Convert.ToInt64(num);
            }
            catch (Exception)
            {
                
            }
            return result;
        }

        /// <summary>
        ///     转数字
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static double ConverToDouble(string num)
        {
            double result;
            try
            {
                result = Convert.ToDouble(num);
            }
            catch (Exception)
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        ///     正常的日期转换只是加了异常处理
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime ConvertToNormalDate(string date)
        {
            DateTime now;
            try
            {
                now = Convert.ToDateTime(date);
            }
            catch (Exception)
            {
                now = DateTime.Now;
            }
            return now;
        }

        /// <summary>
        ///     将时间类型转换为带有时刻的时间字符串如20131118231520
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ConvertDateTimeToString(DateTime dateTime)
        {
            string result;
            try
            {
                result = string.Format("{0}{1}{2}{3}{4}{5}", dateTime.Year.ToString().PadLeft(4, '0'),
                    dateTime.Month.ToString().PadLeft(2, '0'),
                    dateTime.Day.ToString().PadLeft(2, '0'), dateTime.Hour.ToString().PadLeft(2, '0'),
                    dateTime.Minute.ToString().PadLeft(2, '0'),
                    dateTime.Second.ToString().PadLeft(2, '0'));
            }
            catch (Exception)
            {
                result = "20001010101010";
            }
            return result;
        }

        /// <summary>
        ///     将日期转换为20131010
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ConvertDateToString(DateTime date)
        {
            string result;
            try
            {
                result = string.Format("{0}{1}{2}", date.Year.ToString().PadLeft(4, '0'),
                    date.Month.ToString().PadLeft(2, '0'), date.Day.ToString().PadLeft(2, '0'));
            }
            catch (Exception)
            {
                result = "20001010";
            }
            return result;
        }

        /// <summary>
        ///     将年月日20131010或者是20131010101010,转换为日期类型
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ConvertStringToDateTime(string time)
        {
            var result = new DateTime(2000, 10, 10);
            try
            {
                if (time.Length == 14)
                {
                    result = new DateTime(ConvertToInt(time.Substring(0, 4)), ConvertToInt(time.Substring(4, 2)),
                        ConvertToInt(time.Substring(6, 2)), ConvertToInt(time.Substring(8, 2)),
                        ConvertToInt(time.Substring(10, 2)), ConvertToInt(time.Substring(12, 2)));
                }
                else if (time.Length == 8)
                {
                    result = new DateTime(ConvertToInt(time.Substring(0, 4)), ConvertToInt(time.Substring(4, 2)),
                        ConvertToInt(time.Substring(6, 2)));
                }
            }
            catch (Exception)
            {
                result = new DateTime(2010, 10, 10);
            }
            return result;
        }

        /// <summary>
        /// 将source中的\0去掉，主要是写入了cpu卡的
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Replace0Str(string source)
        {
            string result = source;
            try
            {
                result = source.Replace("\0", "");
            }
            catch (Exception)
            {
                result = source;
            }
            return result;
        }
    }
}